import Vue from './runtime/index'
import { query, toDom } from './util'
import { patch } from './runtime/patch'
import { mountComponent } from '../../core/instance/lifecycle'
import { compileToFunctions } from '../../compiler/index'

Vue.prototype.__patch__ =  patch 

// public mount method
Vue.prototype.$mount = function (
  el
) {
   el = el  ? query(el) : undefined
  const options = this.$options
  if(!options.render){
    let {template} =  options
    if(el){
      template = el
    }else if(template){
      if(typeof template === 'string'){
        template = toDom(template)
      }else if(!template.nodeType){
        console.error('template传入的不是DOM元素')
      }else{
        console.error('template模板不合法')
      }
    }else{
      console.error('请提供el或者template模板')
    }
    const {render} = compileToFunctions(this,template)
    options.render = render
  }
  
  return mountComponent(this, el)
}

export default Vue